feat(nextjs): Add signature verification of token passed from middleware - #3121
Merged
Conversation
🦋 Changeset detectedLatest commit: 261639b The changes in this PR will be included in the next version bump. This PR includes changesets to release 7 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
|
@brkalow is this something that only concerns nextjs ? Can't we move this to backend ? |
Member
Author
|
@panteliselef Yeah it's only a Next.js concern, due to the handoff from middleware -> app backend. |
brkalow
marked this pull request as ready for review
April 10, 2024 03:20
nikosdouvlis
approved these changes
Apr 10, 2024
brkalow
force-pushed
the
brk.feat/next-token-signature
branch
from
April 10, 2024 16:27
12d993a to
e58fce0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Introduce signature verification of the JWT that is passed from middleware to the Next.js origin. We're only introducing this functionality into the Next.js SDK as it's the only framework (currently) that involves a network hop between verification of the token and usage.
The goal here was to retain the sync API of
auth(), and so we're not using the native async crypto modules at this time. Instead, we're usingcrypto-js, which exposes sync crpyto APIs.To generate a signature, we're passing the verified JWT and the app's clerk secret key to generate an HMAC with SHA1. The performance impact of this is negligible, with a <1ms measured impact locally. SHA1 is acceptable for use here, as the token has already been verified and we aren't concerned with a collision. We're validating that the token we verified in the middleware is the same one we're using at the app's origin.
This also further mitigates the IDOR vulnerability from earlier this year, ensuring we are always using the previously verified token.
Checklist
npm testruns as expected.npm run buildruns as expected.Type of change